// https://leetcode.cn/problems/subsets/description/

// 算法思路总结：
// 1. 回溯算法生成数组所有子集
// 2. 每个元素有两种选择：包含或不包含
// 3. 递归到数组末尾时保存当前子集
// 4. 先递归不选当前元素，再递归选择当前元素
// 5. 时间复杂度：O(n×2ⁿ)，空间复杂度：O(n)（递归栈深度）

#include <iostream>
using namespace std;

#include <cstring>
#include <vector>
#include <algorithm>

class Solution 
{
public:
    vector<vector<int>> ret;
    vector<int> path;
    int m;
    vector<vector<int>> subsets(vector<int>& nums) 
    {   
        m = nums.size();
        ret.clear();

        dfs(0, nums);

        return ret;
    }

    void dfs(int i, vector<int>& nums)
    {
        if (i == m)
        {
            ret.push_back(path);
            return ;
        }

        dfs(i + 1, nums);

        path.push_back(nums[i]);
        dfs(i + 1, nums);
        path.pop_back();
    }
};

void printResult(const vector<vector<int>>& result) 
{
    cout << "[";
    for (int i = 0; i < result.size(); ++i) 
    {
        cout << "[";
        for (int j = 0; j < result[i].size(); ++j) 
        {
            cout << result[i][j];
            if (j < result[i].size() - 1) cout << ",";
        }
        cout << "]";
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}


int main()
{
    vector<int> nums1 = {1, 2, 3};
    vector<int> nums2 = {0};

    Solution sol;

    auto vv1 = sol.subsets(nums1);
    auto vv2 = sol.subsets(nums2);

    printResult(vv1);
    printResult(vv2);

    return 0;
}